package cn.tworice.admin.service.impl;

import cn.tworice.admin.dao.LogDao;
import cn.tworice.admin.service.LogService;
import cn.tworice.common.entity.Admin;
import cn.tworice.common.entity.AdminLog;
import cn.tworice.common.entity.LoginLog;
import cn.tworice.common.entity.dto.AdminLogDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 异步提交日志信息，以尽量少的影响主进程
 * @Author 二饭
 * @Email 1786705138@qq.com
 * @Date 2021/12/4 18:12
 */
@Service
@EnableAsync
public class LogServiceImpl implements LogService {

    @Autowired
    private LogDao logDao;

    @Async
    @Override
    public void addAdminLog(String adminId,String active) {
        AdminLog adminLog = new AdminLog(UUID.randomUUID().toString().replaceAll("-",""),System.currentTimeMillis(),active,adminId);
        logDao.insertAdminLog(adminLog);
    }

    /**
     * 获取操作日志
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/5 22:01
     */
    @Override
    public List<AdminLogDto> getAdminList(Integer pageSize,Integer page) {
        return logDao.queryAdminList((page*pageSize),pageSize);
    }

    @Override
    public int delAdminLog(String[] id) {

        return logDao.delAdmin(id);
    }

    /**
     * 获取登录日志
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/5 16:41
     */
    @Override
    public List<LoginLog> getLoginList(Integer pageSize,Integer page) {

        return logDao.queryLoginList((page*pageSize),pageSize);
    }

    /**
     * 登录日志记录
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/5 17:09
     */
    @Async
    @Override
    public void addLoginLog(String username, String password, int i) {
        LoginLog log = new LoginLog();
        log.setLoginName(username);
        log.setStatus(i);
        log.setPassWord(password);
        log.setTime(System.currentTimeMillis());
        log.setId(UUID.randomUUID().toString().replaceAll("-",""));
        logDao.insertLoginLog(log);
    }

    /**
     * 获取操作日志总数
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/5 21:27
     */
    @Override
    public Integer getAdminLogTotal() {
        return logDao.getAdminLogTotal();
    }

    @Override
    public Integer getLoginLogTotal() {
        return logDao.getLoginLogTotal();
    }
}
